package com.cy.pj.goods1.dao;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository("dada")
public class DefaultGoodsDao implements GoodsDao{
    @Autowired
    private DataSource dataSource;
    @Override
    public List< Map<String,Object> > findGoods() throws SQLException {
        Connection conn = null;
        Statement stat = null;
        ResultSet rs = null;
        String sql= "select * from tb_goods";
        try{
        conn = dataSource.getConnection();
        stat = conn.createStatement();
        rs = stat.executeQuery(sql);
        List< Map<String,Object> > list = new ArrayList<>();
        while(rs.next()) {
            Map<String, Object> map = new HashMap<>();
            map.put("id", rs.getInt("id"));
            map.put("name", rs.getString("name"));
            map.put("remark", rs.getString("remark"));
            map.put("createdTime", rs.getTimestamp("createdTime"));
            list.add(map);
        }
        return list;
        }catch (SQLException e){
            e.printStackTrace();
            return null;
        }finally {
            if(rs!=null){
                rs.close();
            }
            if(stat!=null){
                stat.close();
            }
            if(conn!=null){
                conn.close();
            }
        }
    }

}
